#include "utils.h"

int getString(char** result, FILE* f)
{
    int chunksize = NUMCARACTERES;
    char *s;
    char *temp;
    int buffersize = chunksize+1;
    int ret = RES_OK;

    s = malloc(buffersize); /* get the first chunk */
    if (s == NULL) {
        return ERR_OUT_OF_MEMORY;
    }

    /* read the first chunk with standard library fgets */
    if (fgets(s, chunksize+1, f) == NULL) {
        ret = ERROR_OR_EOF;
    }
    else {
        while (strchr(s, '\n') == NULL && strchr(s, '\r') == NULL ) {
            buffersize += chunksize;
            temp = realloc(s, buffersize);
            if (temp == NULL) {
                free(s); /* clean up the previously allocated stuff */
                s = NULL;
                ret = ERR_OUT_OF_MEMORY;
                break;
            }
            else {
                s = temp;
                /*read into the zero byte that was the end of the previous chunk */
                if (fgets(s+buffersize-chunksize-1, chunksize+1, f) == NULL) {
                    break;
                }
            }
        }
    }

    /* Elimino los caracteres de retorno y nueva linea */
    *result = s;

    return ret;
}

char* strmcpy(char** s1, const char* s2)
{
    /*if(*s1 != NULL)
        free(s1);*/

    *s1 = (char*)malloc(sizeof(char) * strlen(s2) + 1);
    if(!*s1)
        return NULL;
    strcpy(*s1, s2);
    return *s1;
}

char* strEntre(const char* str, const char a, const char b)
{
    char* start = NULL;
    char* end = NULL;
    char* ret = NULL;
    int n = 0;

    start = strchr(str, a) ? strchr(str, a) + 1 : (char*)str;
    end = strchr(start, b);

    if(end)
        n = end - start;
    else
        n = strlen(start);

    ret = (char*)malloc(sizeof(char) * n + 1);
    memcpy(ret, start, n);
    ret[n] = '\0';

    return ret;
}

int chrAnychr(const char car, const char* patron)
{
    int found = 0;
    int i = 0;
    int n = 0;

    n = strlen(patron);
    while(!found && i<n)
    {
        found = patron[i] == car;
        i++;
    }

    return found;
}

char* cTrim(const char* str, const char* cars)
{
    char* start = NULL;
    char* end = NULL;
    char* trimmed = NULL;
    size_t n = 0;

    /* Trimeo el principio */
    start = (char*)str;
    while( chrAnychr(*start, cars) )
        start++;

    /* Trimeo el final */
    end = start + strlen(start) - 1;
    while( (end >= str) && chrAnychr(*end, cars))
        end--;

    /* Aloco y copio la nueva cadena */
    n = sizeof(char) * (end-start) + 2;
    if(n>0)
    {
        trimmed = (char*)malloc(n);
        memcpy(trimmed, start, n-1);
        trimmed[n-1] = '\0';
        return trimmed;
    }
    else
        return "";
}

bool strIsInt(const char* str)
{
    /* Si es numero o fin de string */
    /*if(*str >= '0' && *str <= '9')
        return strIsInt(str++);
    else if(*str == '\0')
        return true;
*/
    return true;
}
